<!DOCTYPE html>
<html>
<head>
	<title>react-axios</title>
    <meta charset="UTF-8" />
	<script src='../node_modules/react/umd/react.development.js'></script>
	<script src='../node_modules/react-dom/umd/react-dom.development.js'></script>
    <script src='../node_modules/babel-standalone/babel.min.js'></script>
    <script src='../node_modules/axios/dist/axios.js'></script>
</head>
<body>
    <div id="my"></div>
    <script type="text/babel">
    class Hello extends React.Component{
        constructor(){
            super();
            this.state = {
                email:'',
                result:[]
            };
        };
        handleEmail(e){
            var value = e.target.value;
            var filter=/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
            var error='';
            if(!filter.test(value)){
                error="请输入正确的Email"
            };
            this.setState({
                email:value,
                error:error
            })
        }
        submit() {
            alert(JSON.stringify(this.state))
        };
        componentDidMount () {
          this.getList();
        };
        getList(){
            var _this = this;
            axios({
                method:'get',
                url:'http://localhost:3000/info'
            }).then(function(res){
                if(res.data.code =='200'){
                    _this.setState({
                        result:res.data.result
                    })
                }
            }).catch(function(error){
                console.log(error)
            })
        }
        
        render(){ 
            return (
                <div>
                    <p>
                        <label>email:</label>
                        <input type="text"
                          name='email'
                          value={this.state.email} 
                          onChange={(e)=>this.handleEmail(e)}/>
                          <span>{this.state.error}</span>
                    </p>
                    <p>
                        <button onClick={()=>this.submit()}>提交</button>
                        <button onClick={()=>this.getList()}>获取</button>
                    </p>
                    <ul>
                        {
                            this.state.result.map(function(item,index){
                            return <li key={index}>{item.content}</li>
                            })
                        }
                    </ul>
                </div>
                )
        }
    };
    
     ReactDOM.render(
          <Hello/>,
        document.getElementById('my')
    )
    </script>
</body>
</html>